#!/usr/bin/env python
# coding:utf-8
'''
本模块是为了实现添加学生成绩
'''


def add_stu():
    from function import db, cursor
    setcharset = 'set names utf8'
    cursor.execute(setcharset)
    while True:
        try:
            stu_num = int(raw_input('请输入学号：'))
            has_stu = cursor.execute('SELECT id FROM studata WHERE id = %d' % stu_num)
            if has_stu:
                print '该学号已存在，请重新输入！！'
            else:
                name = raw_input('请输入你要添加学生的名字：')
                score = input('请输入该学生语文，数字和英语的成绩，用逗号隔开：')
                chinese, math, english = score
                if (0 <= chinese <= 100) and (0 <= math <= 100) and (0 <= english <= 100) \
                        and (len(name) > 0):
                    sql = "INSERT INTO studata(id, name, chinese, math, english )\
                    VALUES (%d, '%s', %d, %d, %d)" % (stu_num, name, chinese, math, english)
                    cursor.execute(sql)
                    db.commit()
                    print '添加成功，该学生的学号为', stu_num, '该学生名字为', name
                    print '语文:', chinese, ' 数学:', math, ' 英语:', english
                    selt = input('继续请按1，返回请按0 ：')
                    if selt == 0: break;
                else:
                    print '请正确输入学生的名字与成绩。'
        except (TypeError, EOFError, NameError, IOError, SyntaxError, ValueError):
            print '输入错误，请重新输入！！'
            selt1 = input('继续请按1，返回请按0 ：')
            if selt1 == 0: break;
